//
//  LoginController.m
//  BM4GroupDemo
//
//  Created by apple on 16/2/17.
//  Copyright © 2016年 ZR. All rights reserved.
//

#import "LoginController.h"
#import <BMRequest.h>
#import <BMUtils.h>
#import <NSString+Exist.h>
#import <UIViewController+Message.h>
#import <HttpClient.h>
#import <SVProgressHUD.h>

@interface LoginController ()

@end

@implementation LoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"登陆界面";
    self.view.backgroundColor = [UIColor whiteColor];
    
    _nameTF = [UITextField new];
    _nameTF.backgroundColor = [UIColor yellowColor];
    _nameTF.frame = CGRectMake(10, 60, self.view.frame.size.width - 10 *2, 60);
    _nameTF.placeholder = @"请输入用户名";
    [self.view addSubview:_nameTF];

    _passwordTF = [UITextField new];
    _passwordTF.backgroundColor = [UIColor yellowColor];
    _passwordTF.frame = CGRectMake(10, _nameTF.frame.origin.y + 10 + _nameTF.frame.size.height, _nameTF.frame.size.width, _nameTF.frame.size.height);
    _passwordTF.placeholder = @"请输入密码";
    _passwordTF.secureTextEntry = YES;
    [self.view addSubview:_passwordTF];
    
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.backgroundColor = [UIColor blueColor];
    loginBtn.frame = CGRectMake(10, _passwordTF.frame.origin.y + 10 + _passwordTF.frame.size.height,_nameTF.frame.size.width, _nameTF.frame.size.height);
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [self.view addSubview:loginBtn];
    
    [loginBtn addTarget:self action:@selector(loginWithName:password:) forControlEvents:UIControlEventTouchUpInside];
  
}

- (void)loginWithName:(NSString *)name password:(NSString *)password
{
    name = _nameTF.text;
    password = _passwordTF.text;
    
    if (![name isExist]) {
        [SVProgressHUD showInfoWithStatus:@"用户名不能为空"];
        return;
    }
    if (password.length > 16 || password.length < 6) {
        [SVProgressHUD showInfoWithStatus:@"请输入6到16位密码"];
        return;
    }
//    __weak typeof(self)weakSelf = self;
    BMRequest *request = [BMRequest requestWithPath:@"login.htm" contentKey:@"dispatcher"];
    request.params = @{@"username":name,@"password":password,@"role":@1};
    [SVProgressHUD showInfoWithStatus:@"数据加载中..."];
    
    NSURLSession *session = [NSURLSession sharedSession];
    

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
